home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / gg243730.zip / MEMLAB2.C < prev    next >
Text File  |  1993-03-06  |  6KB  |  140 lines

  1. /**********************************************************/
  2. /**********************************************************/
  3. /***                                                    ***/
  4. /***  Program name: MEMLAB2.EXE                         ***/
  5. /***                                                    ***/
  6. /***  Created     : 7. May 1990                         ***/
  7. /***                                                    ***/
  8. /***  Author      : Bo Falkenberg                       ***/
  9. /***                                                    ***/
  10. /***  Revised     : February, 1992 by Darryl Frost      ***/
  11. /***                                                    ***/
  12. /***  Purpose     : To demonstrate the different types  ***/
  13. /***                of memory allocation.               ***/
  14. /***                                                    ***/
  15. /***  Compile     : icc /W2 memlab2.c                   ***/
  16. /***                                                    ***/
  17. /***  Execute     : memlab2 (no commandline parameters) ***/
  18. /***                                                    ***/
  19. /***  Input param : 1. Amount of memory in Kb           ***/
  20. /***                2. Type of memory allocation.       ***/
  21. /***                3. Type of memory usage             ***/
  22. /***                                                    ***/
  23. /**********************************************************/
  24. /**********************************************************/
  25.  
  26. /**********************************************************/
  27. /***  DEFINES                                           ***/
  28. /**********************************************************/
  29. #define INCL_DOSMEMMGR
  30.  
  31. /**********************************************************/
  32. /***  INCLUDE                                           ***/
  33. /**********************************************************/
  34. #include <os2.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. /**********************************************************/
  39. /***  FUNCTION PROTOTYPES                               ***/
  40. /**********************************************************/
  41. void main(int argc, char *argv[], char *envp[]);
  42.  
  43. /**********************************************************/
  44. /***  MAIN PROGRAM                                      ***/
  45. /**********************************************************/
  46. void main(int argc, char *argv[], char *envp[])
  47. {                        /*******************************************/
  48.    PULONG pulBlock;      /* pointer to the starting memory location */
  49.    ULONG ulErr;          /* error variable                          */
  50.    ULONG ulLoop;         /* loop variable                           */
  51.    ULONG ulAmount;       /* amount of memory to be allocated        */
  52.    ULONG ulSelection;    /* input selection                         */
  53.    char cLetter;         /* input char                              */
  54.    BOOL OK = TRUE;       /* memory check indicator                  */
  55.                          /*******************************************/
  56.  
  57.    setbuf(stdout, NULL);
  58.  
  59. /* Read the parameters                                */
  60. /*     1. Amount of memory in Kbytes to be allocated  */
  61. /*     2. Combination of allocation flags             */
  62. /*     3. Whether data should read from or written    */
  63. /*        the memory.                                 */
  64.    printf("How much memory (in Kb) do you want to allocate : ");
  65.    scanf("%u", &ulAmount);
  66.    ulAmount = ulAmount * 1024;
  67.  
  68.    printf("\nWhat type of memory allocation do you want: \n");
  69.    printf("  1. PAG_COMMIT and PAG_READ\n");
  70.    printf("  2. PAG_COMMIT and PAG_WRITE\n");
  71.    printf("  3. PAG_COMMIT and PAG_EXECUTE\n");
  72.    printf("  4. PAG_COMMIT and PAG_GUARD and PAG_WRITE\n");
  73.    printf("  5. PAG_WRITE\n");
  74.    printf("Enter your selection (1, 2, 3, 4 or 5 ) : ");
  75.    scanf("%u", &ulSelection);
  76.  
  77.    printf("\nDo you want to Read or Write in the memory (R/W) : ");
  78.    fflush(stdin);
  79.    scanf("%c", &cLetter);
  80. /* Allocate the memory                                */
  81.    switch (ulSelection)
  82.    {
  83.       case 1:
  84.          ulErr = DosAllocMem ((PPVOID)&pulBlock, ulAmount,
  85.                                PAG_COMMIT | PAG_READ);
  86.          break;
  87.       case 2:
  88.          ulErr = DosAllocMem ((PPVOID)&pulBlock, ulAmount,
  89.                                PAG_COMMIT | PAG_WRITE);
  90.          break;
  91.       case 3:
  92.          ulErr = DosAllocMem ((PPVOID)&pulBlock, ulAmount,
  93.                                PAG_COMMIT | PAG_EXECUTE);
  94.          break;
  95.       case 4:
  96.          ulErr = DosAllocMem ((PPVOID)&pulBlock, ulAmount,
  97.                                PAG_COMMIT | PAG_GUARD | PAG_WRITE);
  98.          break;
  99.       case 5:
  100.          ulErr = DosAllocMem ((PPVOID)&pulBlock, ulAmount,
  101.                                PAG_WRITE);
  102.          break;
  103.       default:
  104.          printf("\nYou made a WRONG selection !!!\n");
  105.          exit (0);
  106.    } /* endswitch */
  107.  
  108.    if (ulErr != 0) {
  109.      printf("Error in allocation : code %u\n", ulErr);
  110.      exit(1);
  111.    } /* endif */
  112.  
  113.    if (cLetter == 'W' || cLetter == 'w')
  114.    {
  115. /* insert data into allocated memory */
  116.       printf("\nWriting...\n");
  117.       for (ulLoop = 0; ulLoop < ulAmount/4; ulLoop++)
  118.       {
  119.          *(pulBlock + ulLoop) = 7;
  120.       } /* endfor */
  121.    } else
  122.    {
  123. /* read data from allocated memory */
  124.       printf("\nReading...\n");
  125.       for (ulLoop = 0; ulLoop < ulAmount/4; ulLoop++)
  126.       {
  127.          if (*(pulBlock + ulLoop) != 7)
  128.          {
  129.             OK = FALSE;
  130.          } /* endif */
  131.       } /* endfor */
  132.    } /* endif */
  133.  
  134.    ulErr = DosFreeMem (pulBlock);
  135.    if (ulErr != 0)
  136.    {
  137.      printf("\nError in freeing : code %u\n", ulErr);
  138.    } /* endif */
  139. }
  140.